/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2014 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/

//***********************************************************************************************************
//  Nuvoton Technology Corp.
//  Date: 23/Jan/2014
//  E-Mail: MicroC-8bit@nuvoton.com
//***********************************************************************************************************
//  Application: GPIO Sample Code
//
//  Output : P1.4 & P2.1 toggle
//***********************************************************************************************************

//------------------------- <<< Use Configuration Wizard in Context Menu >>> --------------------------------
// <h> UART pin Select
//     <o0.6> UART pin
//          <0=> Select P1.0, P1.1 as UART pin(default)
//          <1=> Select P2.6, P2.7 as UART pin(28 pin only)
// </h>
//-------------------------------- <<< end of configuration section >>> -------------------------------------
#define _MAIN_C_
#define Uart_Port_Sel   0x00

#include <stdio.h>
#include "N79E81x.h"
#include "Typedef.h"
#include "Define.h"
#include "Common.h"
#include "Delay.h"
#include "Version.h"
#include "main.h"
#include "adc.h"

 		
//UINT8 code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};   //??
UINT8 code DSY_CODE2[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //??

//dp g f e d c b a
static UINT16 SOUND_100US=0;
static UINT8 SYSTEM_TIME_100US=0;
static UINT8 SYSTEM_TIME_1mS=0;
static UINT8 SYSTEM_TIME_10mS=0;
static UINT8 SYSTEM_TIME_50mS=0;
static UINT8 SYSTEM_TIME_500mS=0;
static UINT8 led_cnt = 1;
//#ifdef FOSC_221184
    #define TH0_Value    256-184      //18*12/22.1184 = 10 us   125
    #define TL0_Value     256-184     //1843*12/22.1184 = 1 ms
//#endif
//#define  TH0_Value 0x48					// 11.0592MHz ?? ??187us(0x53)   	// 22.1184MHz ????100us (0x48),   50us(0xA4)
//#define  TL0_Value 0x48					// ???0 (8 ?????), 12T

void set_pin(UINT8 number){
    LEDa=number&0x01;
	LEDb=number&0x02;
	LEDc=number&0x04;
	LEDd=number&0x08;
	LEDe=number&0x10;
	LEDf=number&0x20;
	LEDg=number&0x40;
	
}

void clean_pin(){
    LEDa=0;
	LEDb=0;
	LEDc=0;
	LEDd=0;
	LEDe=0;
	LEDf=0;
	LEDg=0;
    LEDdp=0;
	LED1=1;
	LED2=1;
	LED3=1;
	
}

//????
void show_screen(UINT16 showNumber,UINT8 mode){ //0 ????, 1 ???? 2 ???? 
	UINT8 number;
	clean_pin();
	if (led_cnt>4) led_cnt = 1;
	switch(led_cnt)
		{
		case 1:
		if(showNumber>99){
			 LED1 =0;//
		}
		number=DSY_CODE2[showNumber/100%10];
		set_pin(number);
		break;
		case 2:

		if(showNumber>9) LED2 =0;
		number=DSY_CODE2[showNumber/10%10];
		set_pin(number);
		break;
        case 3:
		LED3=0; 
		number=DSY_CODE2[showNumber%10];
		set_pin(number);
		break;
	
		case 4:
		if(mode == 1){
			LEDdp=1;
			LED3=0;
		}else if (mode == 2){
			LEDdp=1;
			LED2=0;
		}else if (mode == 3){
			LEDdp=1;
			LED3=0;
			LED2=0;
		}
		
		break;
		
	}
	
	led_cnt++;
}

 void Timer1_ISR(void) interrupt 3   // Vector @  0x0B   100us 
{
    TF1 = 0;                        // Reset timer0 interrupt flag (already done by hardware)
  SYSTEM_TIME_100US++;
/*
if(check_state==1&&DATA_PIN){
	SOUND_100US++;
	check_state=2;
}

if(check_state==2&&!DATA_PIN){
    check_state=0;
	   now_temp = SOUND_100US;
		distance = SOUND_100US*1.7;
		SOUND_100US=0;
   }*/
	if(SYSTEM_TIME_100US==10)	 //  10= 1MS
	{  
		SYSTEM_TIME_100US=0;
		sk_1ms_flag=1;
		SYSTEM_TIME_1mS++;
if(SYSTEM_TIME_1mS==10){
	sk_10ms_flag=1;
	SYSTEM_TIME_1mS=0;
	SYSTEM_TIME_10mS++;
		if (SYSTEM_TIME_10mS == 5)
			{
				SYSTEM_TIME_10mS=0;
				SYSTEM_TIME_50mS++;
				sk_50ms_flag=1;
				if(SYSTEM_TIME_50mS==10)
					{
						SYSTEM_TIME_50mS=0;
						sk_500ms_flag=1;
						SYSTEM_TIME_500mS++;
							if(SYSTEM_TIME_500mS==2)
								{
									sk_1s_flag=1;
									SYSTEM_TIME_500mS=0;
						
								}
					}
			}
	}
	}
}

	void SystemInit(void)
{
	  EA = 0;                         // Enable interrupts		// ????
	  ADC_Init();
   TMOD |= 0x20; 
	 TMOD |= 0x01;
   TH1 = TH0_Value;                // Initial values	 // 22.1184MHz ?? ??100us
   TL1 = TL0_Value;
	ET1 = 1;                        // Enable timer0 interrupt	// ????0??
	EA = 1;                         // Enable interrupts		// ????
	TR1 = 1;                        // Timer0 run	 // ???0????


}
//-----------------------------------------------------------------------------------------------------------
void main(void)
{
	SystemInit();
	init_state();
    //PWM_Channel(PWM_CHANNEL3);           // Select PWM channel.
  //  Init_PWM();                        // Enable PWM function and set PWM period.
 
	#ifdef hight_gpio   // 10 11 12 13 14 16     05 06 07   30 34 ????   p03????  p00pmw??
	P0M1=0X04;   //03 ????
	P0M2|=0Xe0;//05 06 07 ??   

	P1M1=0X00;
	P1M2|=0Xff; //????

	P2M1&=0X00;
	P2M2|=0Xf0;   

  P3M1&=0X00;
	P3M2|=0X18;  //30 34 ?? 
  #endif 

    while(1)
    {
    Function_1msProcess();	
	Function_10msProcess();
	Function_50msProcess();  
	Function_500msProcess();	
	Function_1sProcess(); 	
    }
}

#undef _MAIN_C_

//-----------------------------------------------------------------------------------------------------------
